package com.organization

import grails.test.*

class CommonServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testCommonServiceMethods() {
        //test convertDateFromJson(String date)
        def testService = new CommonService()
        assertEquals "Wed Jun 27 00:00:00 MSK 2012", testService.convertDateFromJson("2012-06-27").toString()
        assertNull testService.convertDateFromJson("")
        assertNull testService.convertDateFromJson(null)

        mockDomain(Department)
        def testDep = new Department(name:"D")
        assertFalse testDep.validate()
        assertTrue testDep.hasErrors()

        //test parseErrorsFromDomain(Map errors)
        def mockControl = mockFor(CommonService)
        mockControl.demand.getMessage(1..1) {errorValue, locale->
            "test error"
        }
        testService.messageSource = mockControl.createMock()
        def parsedErrors = testService.parseErrorsFromDomain(testDep.errors)
        assertNotNull parsedErrors
        assertFalse parsedErrors.success
        assertEquals 2, parsedErrors.size()
        assertEquals 1, parsedErrors.errors.size()
        assertEquals "test error", parsedErrors.errors.name

        //test nodeIdToDbId(String nodeId)
        assertEquals "1234",testService.nodeIdToDbId("Project_1234")
        assertEquals "123",testService.nodeIdToDbId("Department_123")
        assertEquals "12",testService.nodeIdToDbId("Employee_12")

        //verify count of mocked methods call
        mockControl.verify()
    }
}
